﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity;
using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace HobSoft.SoccerBet2014.Common.Wpf.Behavior
{
    public static class CheckedClick
    {
        private static readonly DependencyProperty CheckedClickBehaviorProperty =
            DependencyProperty.RegisterAttached("CheckedClickBehavior", typeof(CheckedClickBehavior), typeof(CheckedClick), null);

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(CheckedClick), new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(CheckedClick), new PropertyMetadata(OnSetCommandParameterCallback));

        public static void SetCommand(ToggleButton control, ICommand command)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            control.SetValue(CheckedClick.CommandProperty, command);
        }

        public static ICommand GetCommand(ToggleButton control)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            return control.GetValue(CheckedClick.CommandProperty) as ICommand;
        }

        public static void SetCommandParameter(ToggleButton control, object parameter)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            control.SetValue(CheckedClick.CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(ToggleButton control)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            return control.GetValue(CheckedClick.CommandParameterProperty);
        }

        private static CheckedClickBehavior GetOrCreateBehavior(ToggleButton control)
        {
            CheckedClickBehavior checkedCommandBehavior = control.GetValue(CheckedClick.CheckedClickBehaviorProperty) as CheckedClickBehavior;
            if (checkedCommandBehavior == null)
            {
                checkedCommandBehavior = new CheckedClickBehavior(control);
                control.SetValue(CheckedClick.CheckedClickBehaviorProperty, checkedCommandBehavior);
            }
            return checkedCommandBehavior;
        }


        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var control = dependencyObject as ToggleButton;
            if (control == null) return;
            var behavior = GetOrCreateBehavior(control);
            behavior.Command = e.NewValue as ICommand;
        }

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var control = dependencyObject as ToggleButton;
            if (control == null) return;
            var behavior = GetOrCreateBehavior(control);
            behavior.CommandParameter = e.NewValue;
        }
    }

    public class CheckedClickBehavior : CommandBehaviorBase<ToggleButton>
    {
        public CheckedClickBehavior(ToggleButton obj)
            : base(obj)
        {
            obj.Click += CheckButton_Click;
        }

        private void CheckButton_Click(object sender, RoutedEventArgs e)
        {
            ExecuteCommand(CommandParameter);
        }

        private void CheckButton_Checked(object sender, RoutedEventArgs e)
        {
            ExecuteCommand(CommandParameter);
        }
    }
}
